﻿insert [Application] values('Citi Scheduler', 'Sch', 'Sch')

insert into EmailData values(1, @@IDENTITY,'d157951e-5748-4738-9980-2e01c1f91076',
 '[WebUrl]/RoutineError.aspx/ErrorEmail?routineExecutionId=[RoutineExecutionId]', 'Routine Error: [RoutineError]',0, null, 0, 1, 'EmailErroMonitor')

insert into EmailDataParameter values ('[WebUrl]', 7)
insert into EmailDataParameter values ('[RoutineExecutionId]', 7)
insert into EmailDataParameter values ('[RoutineError]', 7)

insert into EmailData values(1, 2,'d945d9fe-6d1b-402b-ba9b-924eda8dcb69', 
'[WebUrl]/RoutineExecution.aspx/EmailDashboard?emailScheduleId=[EmailScheduleId]', 
'Dashboard: [CategoryName] at [Date]',0, null, 0, 1, 'DashboardScheduledMonitor')

insert into EmailDataParameter values ('[WebUrl]', 8)
insert into EmailDataParameter values ('[EmailScheduleId]', 8)
insert into EmailDataParameter values ('[CategoryName]', 8)
insert into EmailDataParameter values ('[Date]', 8)	




insert into routineerrorhistory ([Date],
[Description],
UserId,
RoutineErrorId,
[Status])
select GETDATE(), 'Error is opened and waiting for action.', Routine.ResponsibleId, RoutineError.Id,0
from RoutineError
inner join RoutineExecution on RoutineExecution.Id = RoutineError.RoutineExecutionId
inner join Routine on Routine.Id = RoutineExecution.RoutineId


--create table OnBehalfOf
--(
--	Id int identity
--	,FriendlyName varchar(255) not null
--	,Email varchar(255) not null
--	,ResponsibleId int not null
--	,Active bit not null
	
--	primary key(Id)
--	,foreign key(ResponsibleId) references User(Id)
--)
--GO

--create table EmailData
--(
--	[Id] [int] IDENTITY(1,1) NOT NULL,
--	[OnBehalfOfId] [int] NOT NULL,
--	ApplicationId int not null,
--	ApplicationKey varchar(200) not null,
--	Url varchar(1500) null,
--	[Subject] [varchar](255) NOT NULL, 
--	[Importance] [int] NOT NULL,
--	[Body] [text]  NULL,
--	Template bit not null 
--	,Active bit not null
	
--	primary key(Id)
--	, FOREIGN KEY([OnBehalfOfId]) REFERENCES [dbo].[OnBehalfOf] ([Id])
--	, FOREIGN KEY(ApplicationId) REFERENCES [dbo].Application ([Id])
--)
--GO

--CREATE TABLE [dbo].[Email](
--	[Id] [int] IDENTITY(1,1) NOT NULL,
--	ApplicationUniqueKey varchar(200) not null,
--	EmailDataId int not null,
--	[SentOn] [datetime] NULL,
--	[CreatedDate] [datetime] NOT NULL,
--	[Tentatives] [int] NOT NULL
	
--	primary key(Id)
--	, FOREIGN KEY(EmailDataId) REFERENCES [dbo].EmailData ([Id])
--)
--GO

--CREATE TABLE [dbo].[EmailAddress](
--	[Id] [int] IDENTITY(1,1) NOT NULL,
--	[Address] [varchar](500) NOT NULL,
--PRIMARY KEY (Id)
--)
--go

--CREATE TABLE [EmailReceiver](
--	[Id] [int] IDENTITY(1,1) NOT NULL,
--	[EmailId] [int] NOT NULL,
--	[EmailAddressId] [int] NOT NULL,
--	[Type] [int] NOT NULL
	
--	primary key(Id)
--	,FOREIGN KEY([EmailId]) REFERENCES [dbo].[Email] ([Id])
--	,FOREIGN KEY([EmailAddressId]) REFERENCES [dbo].[EmailAddress] ([Id])
--	)
--GO

--CREATE TABLE [dbo].[Attachment](
--	[Id] [int] IDENTITY(1,1) NOT NULL,
--	[EmailId] [int] NOT NULL,
--	[Path] [varchar](500) NOT NULL
--	, FriendlyName varchar(100) not null
	
--	primary key(Id)
--	,FOREIGN KEY([EmailId]) REFERENCES [dbo].[Email] ([Id])
--)
--GO

--alter table RoutineParameter
--add EmailId int null
--go

--alter table RoutineParameter
--add foreign key (EmailId) references Email(id)
--GO

--create table DownloadAttachment
--(
--	[Id] [int] IDENTITY(1,1) NOT NULL,
--	ApplicationId int not null,
--	ApplicationUniqueKey varchar(200) not null,
--	[Subject] varchar(200) not null,
--	[From] varchar(200) null,
--	InitialDate datetime null
--	,DefaultFolder int not null
--	,DownloadedOn datetime null
--	,DownloadFolder varchar(200) not null
	
--	primary key(Id)
--	, FOREIGN KEY(ApplicationId) REFERENCES [dbo].Application ([Id])
--)